package com.zsr.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsr.example.config.annotation.RequiresPermissions;
import com.zsr.example.entity.*;
import com.zsr.example.service.*;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.util.constants.BusinessEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 步骤-课程关联表 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2022-02-10
 */
@RestController
@RequestMapping("/step-relation")
public class EduPathStepRelationController {

    @Autowired
    private EduPathStepRelationService relationService;

    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduArticleService articleService;
    @Autowired
    private EduPathService pathService;
    @Autowired
    private EduPathStepService stepService;

    @RequiresPermissions("path:list")
    @GetMapping("/pageListStepRelationCondition/{sid}/{current}/{limit}")
    public JSONObject listArticle(@PathVariable String sid,
                                  @PathVariable long current,
                                  @PathVariable long limit) {
        Page<EduPathStepRelation> page = new Page<>(current, limit);

        QueryWrapper<EduPathStepRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("step_id", sid);
        relationService.page(page, queryWrapper);
        //List<EduPathStepRelation> records = page.getRecords().stream().map(x -> x.setTypeText(BusinessEnum.getDesc(x.getTargetType()))).collect(Collectors.toList());
        page.getRecords().forEach(x -> x.setTypeText(BusinessEnum.getDesc(x.getTargetType())));
        return CommonUtil.successPage(page.getTotal(), page.getRecords());
    }

    @RequiresPermissions("path:update")
    @GetMapping("/getRelation/{rid}")
    public JSONObject getRelation(@PathVariable String rid) {
        EduPathStepRelation relation = relationService.getById(rid);
        return CommonUtil.successJson(relation);

    }

    @RequiresPermissions("path:list")
    @GetMapping("/getCourse")
    public JSONObject getCourse() {
        /*List list = new ArrayList();
        BusinessEnum businessEnum= BusinessEnum.values()[type];
        switch (businessEnum){
            //switch的case必须是在编译期就能获取到的值,所以获取枚举内的变量，因为在运行期才能获取到值,不能通过编译
            //case BusinessEnum.COURSE.getType():
            case COURSE:
                list = courseService.list(null);
                break;
            case ARTICLE:
                list = articleService.list(null);
                break;
            default:
                break;
        }*/
        Map<String, Object> map = new HashMap<>();
        List<EduCourse> vod = courseService.list(null);
        List<EduArticle> articles = articleService.list(null);
        map.put("vod", vod);
        map.put("articles", articles);

        return CommonUtil.successJson(map);
    }

    @RequiresPermissions("path:update")
    @PostMapping("/saveOrUpdateRelation")
    public JSONObject saveOrUpdateRelation(@RequestBody EduPathStepRelation relation) {
        //修改path课程数
        if (StringUtils.isEmpty(relation.getId())) {
            EduPathStep step = stepService.getById(relation.getStepId());
            EduPath path = pathService.getById(step.getPathId());
            path.setCoursesCount(path.getCoursesCount() + 1);
            pathService.update(path, null);
        }
        relationService.saveOrUpdate(relation);
        return CommonUtil.successJson();
    }

}

